package com.leetcodehot.problems;

import java.util.Arrays;

public class problems64 {
    private int m;
    private int n;
    private int[][] grid;

    public int minPathSum(int[][] grid) {
        this.m = grid.length;
        this.n = grid[0].length;
        this.grid = grid;
        int[][] memo = new int[m + 1][n + 1];
        for (int i = 0; i <= m; i++) {
            memo[i][0] = Integer.MAX_VALUE;
        }
        for (int i = 0; i <= n; i++) {
            memo[0][i] = Integer.MAX_VALUE;
        }
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (i == 1 && j == 1) {
                    memo[i][j] = grid[i - 1][j - 1];
                } else {
                    memo[i][j] = Math.min(memo[i - 1][j], memo[i][j - 1]) + grid[i - 1][j - 1];
                }
            }
        }
        return memo[m][n];
    }
}
